package com.thinkfit.jy.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.common.lib.manager.DataManager;
import com.inuker.bluetooth.library.Constants;
import com.inuker.bluetooth.library.search.SearchResult;
import com.thinkfit.jy.R;
import com.thinkfit.jy.bluetooth.Bluetooth;
import com.thinkfit.jy.utils.BtUtil;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

public class BluetoothAdapter extends BaseQuickAdapter<SearchResult, BaseViewHolder> {

    private Context mContext;
    private int mMainStyle;

    private HashMap<String, Integer> mMap;

    public BluetoothAdapter(Context context, int type) {
        super(R.layout.item_bluetooth);
        mContext = context;
        mMainStyle = DataManager.Companion.getInstance().getMainStyle();
        mMap = new HashMap<>();
    }


    @SuppressLint("MissingPermission")
    @Override
    protected void convert(@NotNull BaseViewHolder helper, SearchResult bean) {
        //0 未知类型  1 传统类型  2 ble 类型  3  传统和ble双重类型 蓝牙型号
        int rid=R.mipmap.app_logo;
        int type = bean.device.getType();
        switch (type){
            case 1:
                rid=R.drawable.app_bluetooth_on;
                break;
            case  2:
//                rid = android.R.drawable.stat_sys_phone_call;
                rid= BtUtil.getDeviceType(bean.device.getBluetoothClass());
                break;
                
        }

        int state;
        if (mMap.containsKey(bean.getAddress())) {
            state = mMap.get(bean.getAddress());
        } else {
            state = Bluetooth.getConnectStatus(bean.getAddress());
        }
        TextView tvName = helper.getView(R.id.tvName);
        tvName.setTextColor(mContext.getColor(R.color.white));
        String text = mContext.getString(R.string.app_not_connect);
        if (state == android.bluetooth.BluetoothAdapter.STATE_CONNECTED) {
            text = mContext.getString(R.string.app_connected);
            tvName.setTextColor(mContext.getColor(R.color.color_3D5AFE));
        } else if (state == android.bluetooth.BluetoothAdapter.STATE_CONNECTING) {
            text = mContext.getString(R.string.app_connecting);
        }
        helper.setText(R.id.tvStatus,text);

        rid = BtUtil.getDeviceType(bean.device.getBluetoothClass());

        //设备型号
        helper.setBackgroundResource(R.id.ioc_bluetooth, rid);
        String name = bean.getName();
        helper.setText(R.id.tvName, TextUtils.isEmpty(name) || name.equals("NULL") ? bean.getAddress() : name)
                .setGone(R.id.tvBonding, bean.device.getBondState() != Constants.BOND_BONDING)
                .setBackgroundResource(R.id.line, mMainStyle == 3 ? com.common.lib.R.color.line4 : com.common.lib.R.color.line)
                .setGone(R.id.line, getItemPosition(bean) == getItemCount() - 1);
    }

    public void addData(SearchResult bean) {
        List<SearchResult> list = getData();
        for (SearchResult result : list) {
            if (result.getAddress().equals(bean.getAddress())) {
                return;
            }
        }
        super.addData(bean);
    }

    public void changStatus(String address, int status) {
        mMap.put(address, status);
    }

    public void removeDevice(String address) {
        List<SearchResult> list = getData();
        for (SearchResult result : list) {
            if (result.getAddress().equals(address)) {
                remove(result);
                break;
            }
        }
    }

}
